package collection;
import java.util.*;
public class ListDemo02 {
    public static void main(String[] args) {
       List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        System.out.println("初始字符串list:"+list);
        String sss = list.get(2);
        System.out.println("sss="+sss);//three
        String ss = list.set(4,"six");
        System.out.println("ss="+ss);//five
        System.out.println("set方法修改后的list="+list);//[one, two, three, four, six]
        String s = list.remove(4);
        System.out.println(s);//six
        System.out.println("remove方法删除下表为4的元素后list="+list);//[one, two, three, four]
        list.add(0,"zero");
        System.out.println(list);//[zero, one, two, three, four]
        List<String> list1 = list.subList(1,4);
        System.out.println(list1);//[one, two, three]

        Random rand = new Random();
        List<Integer> listi = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            listi.add( rand.nextInt(100));
        }
        System.out.println("listi"+listi);
        Collections.sort(listi);//自然排序(从小到大)
        System.out.println("listi排序后的数据:"+listi);//有序(小到大)

        //字符串排序 String
        List<String> lists = new ArrayList<>();
        lists.add("张三");
        lists.add("李四四");
        lists.add("王五五五");
        Collections.sort(lists, new Comparator<String>() {
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        });
        System.out.println("字符串升序后为:"+lists);

        Collections.sort(lists,((o1, o2) -> o1.length()-o2.length() ));
        System.out.println("Lambda表达式写法:"+lists);

        List<Point>  listp = new ArrayList<>();
        listp.add(new Point(5,8));
        listp.add(new Point(15,60));
        listp.add(new Point(57,89));
        listp.add(new Point(1,4));
        listp.add(new Point(10,8));
        listp.add(new Point(22,35));
        System.out.println("list原始数据为:"+listp);
        Collections.sort(listp, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int len1= o1.getX()*o1.getX()+o1.getY()* o1.getY();
                int len2= o2.getX()*o2.getX()+o2.getY()* o2.getY();
                return len1-len2;
            }
        });//升序
        System.out.println(listp);//[(1,4), (5,8), (10,8), (22,35), (15,60), (57,89)]
        listp.sort( ( o1,  o2) ->{
            int len1= o1.getX()*o1.getX()+o1.getY()* o1.getY();
            int len2= o2.getX()*o2.getX()+o2.getY()* o2.getY();
            return len1-len2;
        });
        System.out.println("Lambda表达式排序:"+listp);

        List<String> listl = new ArrayList<>();
        listl.add("two");
        listl.add("two");
        listl.add("three");
        listl.add("four");
        listl.add("five");
        listl.add("five");
        System.out.println("listl:"+listl);//[two, two, three, four, five, five]

        Set<String> set = new HashSet<>();
        set.addAll(listl);
        System.out.println("set:"+set);//[four, two, three, five]
    }
}
